package com.fresh.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fresh.common.ResponseResult;
import com.fresh.entity.FreshGoods;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ExcelUtil {

    @Autowired
    private ApplicationContext applicationContext;

    public <T> T convertTypeToObject(Class<T> c){
        return applicationContext.getBean(c);
    }

    public static <T> List<T> convertObjectToList(Object object, Class<T> c) {
        List<T> result = new ArrayList<>();
        if (object instanceof ArrayList<?>) {
            for (Object o : (ArrayList<?>) object) {
                String json = JSON.toJSONString(o);
                T t = JSONObject.parseObject(json, c);
                result.add(c.cast(t));
            }
            return result;
        }
//       if (object instanceof ResponseResult){
//           return c.cast(((ResponseResult) object).getData());
//       }
        return null;
    }

}
